package com.ebupt.migu.music.bannertwo.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ebupt.migu.music.bannertwo.entity.*;
import com.ebupt.migu.music.bannertwo.service.IBannerTwoCheckService;
import com.ebupt.migu.music.bannertwo.service.IBannerTwoDetailService;
import com.ebupt.migu.music.bannertwo.service.IBannerTwoInfoService;
import com.ebupt.migu.music.bannertwo.service.IBannerTwoOperateService;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.EmptyUtils;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.media.entity.MediaCheck;
import lombok.extern.slf4j.Slf4j;

import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/bannerTwoInfo")
@Slf4j
public class BannerTwoInfoController {
    @Autowired
    private IBannerTwoInfoService bannerTwoInfoService;
    @Autowired
    private IBannerTwoDetailService bannerTwoDetailService;
    @Autowired
    private IBannerTwoOperateService bannerTwoOperateService;
    @Autowired
    private IBannerTwoCheckService checkService;

    /**
     * 分页列表查询
     *
     * @param bannerTwoInfo
     * @param req
     * @return
     */
    @PostMapping(value = "/list")
    public ResultObject<IPage<BannerTwoInfo>> queryPageList(@RequestBody BannerTwoInfoPage bannerTwoInfo,
//                                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//                                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                            HttpServletRequest req) {
        QueryWrapper<BannerTwoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                //  .eq(!EmptyUtils.isEmpty(bannerTwoInfo.getId()), BannerTwoInfo::getId, bannerTwoInfo.getId())
                .like(!EmptyUtils.isEmpty(bannerTwoInfo.getBannerName()), BannerTwoInfo::getBannerName, bannerTwoInfo.getBannerName())
                .eq(!EmptyUtils.isEmpty(bannerTwoInfo.getExecutionType()), BannerTwoInfo::getExecutionType, bannerTwoInfo.getExecutionType())
                .eq(!EmptyUtils.isEmpty(bannerTwoInfo.getCreatorName()), BannerTwoInfo::getCreatorName, bannerTwoInfo.getCreatorName())
                .eq(!EmptyUtils.isEmpty(bannerTwoInfo.getCreatorDepartment()), BannerTwoInfo::getCreatorDepartment, bannerTwoInfo.getCreatorDepartment())
                .eq(!EmptyUtils.isEmpty(bannerTwoInfo.getExecutionStatus()), BannerTwoInfo::getExecutionStatus, bannerTwoInfo.getExecutionStatus())
                .between(!EmptyUtils.isEmpty(bannerTwoInfo.getSTime()) && !EmptyUtils.isEmpty(bannerTwoInfo.getETime()), BannerTwoInfo::getCreateTime, bannerTwoInfo.getSTime(), bannerTwoInfo.getETime())
                .between(!EmptyUtils.isEmpty(bannerTwoInfo.getStartTime()) && !EmptyUtils.isEmpty(bannerTwoInfo.getEndTime()), BannerTwoInfo::getStartTime, bannerTwoInfo.getStartTime(), bannerTwoInfo.getEndTime())
                .between(!EmptyUtils.isEmpty(bannerTwoInfo.getStartTime()) && !EmptyUtils.isEmpty(bannerTwoInfo.getEndTime()), BannerTwoInfo::getEndTime, bannerTwoInfo.getStartTime(), bannerTwoInfo.getEndTime())
                .orderByDesc(BannerTwoInfo::getCreateTime);
        Page<BannerTwoInfo> page = new Page<BannerTwoInfo>(bannerTwoInfo.getPageNo(), bannerTwoInfo.getPageSize());
        IPage<BannerTwoInfo> pageList = bannerTwoInfoService.page(page, queryWrapper);
        return ResultObject.ok(pageList);
    }

    /**
     * 添加
     *
     * @param bannerTwoInfoPage
     * @return
     */
    @PostMapping(value = "/add")
    public ResultObject<String> add(@RequestBody BannerTwoInfoPage bannerTwoInfoPage) {
        BannerTwoInfo bannerTwoInfo = new BannerTwoInfo();
        String s = "BL" + IdUtil.getSnowflakeNextId();
        BeanUtils.copyProperties(bannerTwoInfoPage, bannerTwoInfo);
        long count = bannerTwoInfoService.count(new QueryWrapper<BannerTwoInfo>().lambda().eq(BannerTwoInfo::getBannerName, bannerTwoInfo.getBannerName()));
        if (count > 1) {
            return ResultObject.error("策略名称已存在");
        }
        bannerTwoInfo.setId(s);
        bannerTwoInfoService.saveMain(bannerTwoInfo, bannerTwoInfoPage.getBannerTwoDetailList());
        return ResultObject.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param bannerTwoInfoPage
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.POST})
    public ResultObject<String> edit(@RequestBody BannerTwoInfoPage bannerTwoInfoPage) {
        BannerTwoInfo bannerTwoInfo = new BannerTwoInfo();
        BeanUtils.copyProperties(bannerTwoInfoPage, bannerTwoInfo);
        BannerTwoInfo bannerTwoInfoEntity = bannerTwoInfoService.getById(bannerTwoInfo.getId());
        if (bannerTwoInfoEntity == null) {
            return ResultObject.error("未找到对应数据");
        }
        bannerTwoInfoService.updateMain(bannerTwoInfo, bannerTwoInfoPage.getBannerTwoDetailList());
        return ResultObject.ok("编辑成功！");
    }

    /**
     * 通过id删除
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/delete")
    public ResultObject<String> delete(HttpServletRequest request) {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        bannerTwoInfoService.delMain(params);
        return ResultObject.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public ResultObject<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.bannerTwoInfoService.delBatchMain(Arrays.asList(ids.split(",")));
        return ResultObject.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public ResultObject<BannerTwoInfo> queryById(@RequestParam(name = "id", required = true) String id) {
        BannerTwoInfo bannerTwoInfo = bannerTwoInfoService.getById(id);
        if (bannerTwoInfo == null) {
            return ResultObject.error("未找到对应数据");
        }
        return ResultObject.ok(bannerTwoInfo);

    }

    /**
     * 通过bannerid查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryBannerTwoDetailByMainId")
    public ResultObject<List<BannerTwoDetail>> queryBannerTwoDetailListByMainId(@RequestParam(name = "id", required = true) String id) {
        List<BannerTwoDetail> bannerTwoDetailList = bannerTwoDetailService.selectByMainId(id);
        return ResultObject.ok(bannerTwoDetailList);
    }

    /**
     * 优先级排序
     *
     * @param
     * @return
     */
    @RequestMapping("/priority")
    public ResultObject priority(HttpServletRequest request) {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return bannerTwoInfoService.priority(params);
    }

    /**
     * 配置信息
     *
     * @param
     * @return
     */
    @RequestMapping("/configuration")
    public ResultObject configuration() {
        return bannerTwoInfoService.getConditions();
    }

    @RequestMapping("/updateStatus")
    public ResultObject updateStatus(BannerTwoInfoPage bannerTwoInfoPage, HttpServletRequest request) {
        BannerTwoInfo bannerTwoInfo = new BannerTwoInfo();
        BeanUtils.copyProperties(bannerTwoInfoPage, bannerTwoInfo);
        bannerTwoInfoService.updateById(bannerTwoInfo);
        if ("8".equals(bannerTwoInfo.getExecutionStatus())) {
            Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
            BannerTwoOperate operate = new BannerTwoOperate();
            operate.setBannerId(bannerTwoInfo.getId());
            operate.setOperateName((String) params.get("creatorName"));
            operate.setOperateLogin((String) params.get("creatorId"));
            operate.setDepartment((String) params.get("creatorDepartment"));
            operate.setOperateType("1");
            bannerTwoOperateService.save(operate);
            checkService.update(new UpdateWrapper<BannerTwoCheck>().lambda().eq(BannerTwoCheck::getBannerTwoId, bannerTwoInfo.getId()).set(BannerTwoCheck::getDeleteType, 1));

        }
        if ("5".equals(bannerTwoInfo.getExecutionStatus())) {
            checkService.update(new UpdateWrapper<BannerTwoCheck>().lambda().eq(BannerTwoCheck::getBannerTwoId, bannerTwoInfo.getId()).set(BannerTwoCheck::getDeleteType, 1));
        }

        return ResultObject.ok("修改成功！");
    }
}
